<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Db;
class Bankcard extends Common
{
    //绑定银行卡接口
	public function index(Request $request)
	{
		$name = $request->param('name');
		$card = $request->param('card');
        $uid = $request->param('uid');
		if(empty($name) || empty($card)){
      		$arr['status'] = 30001;
      		$arr['msg'] = "参数错误";
      	}else{
      		$host = "https://aliyun-bankcard-verify.apistore.cn";
            $path = "/bank";
            $method = "GET";
            $appcode = "26b627ad1a9549b181600362d852592c";
            $headers = array();
            array_push($headers, "Authorization:APPCODE " . $appcode);
            $querys = "bankcard=$card&realName=$name";
            $bodys = "";
            $url = $host . $path . "?" . $querys;

            $curl = curl_init();
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($curl, CURLOPT_FAILONERROR, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            if (1 == strpos("$".$host, "https://"))
            {
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            }
            $res=curl_exec($curl);
            if ($res) {
                $info=json_decode($res,1);
                $bankname=$info['result']['information']['bankname'];
                $data=[
                'uid' => $uid,
                'bankName' => $bankname,
                'cardNumber' => $info['result']['bankcard']
                ];
                $result=Db::name('bankcard')->insert($data);
                if($result){
                	$arr['status'] = 200;
                	$arr['msg'] = '绑定成功';
                }
            }
      	}
      	echo callback(self::$callback,$arr);
	}
    public function getCard(Request $request)
    {
        $uid = $request->param('uid');
        $sql="select * from bankcard where uid = $uid";
        $data = Db::query($sql);
        foreach($data as $k=>$v){
            $data[$k]['cardNumber'] = substr($v['cardNumber'], 0, 4).'********'.substr($v['cardNumber'], 12);
        }
        //print_r($data);die;
        if(empty($data)){
            $arr['status'] = 300;
            $arr['msg'] = "您还没有绑定银行卡";
        }else{
            $arr['status'] = 200;
            $arr['msg'] = '成功';
            $arr['data'] = $data;
        }
        echo callback(self::$callback,$arr);
    }
}
